package vip.xiaonuo.supplier.modular.base.delivery.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.base.delivery.entity.SupplierDelivery;
import vip.xiaonuo.supplier.modular.base.delivery.mapper.SupplierDeliveryMapper;
import vip.xiaonuo.supplier.modular.base.delivery.param.SupplierDeliveryAddParam;
import vip.xiaonuo.supplier.modular.base.delivery.param.SupplierDeliveryEditParam;
import vip.xiaonuo.supplier.modular.base.delivery.param.SupplierDeliveryIdParam;
import vip.xiaonuo.supplier.modular.base.delivery.param.SupplierDeliveryPageParam;
import vip.xiaonuo.supplier.modular.base.delivery.service.SupplierDeliveryService;

import java.util.List;

/**
 * 发货方式Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/15 19:07
 **/
@Service
public class SupplierDeliveryServiceImpl extends ServiceImpl<SupplierDeliveryMapper, SupplierDelivery> implements SupplierDeliveryService {

    @Override
    public Page<SupplierDelivery> page(SupplierDeliveryPageParam supplierDeliveryPageParam) {
        QueryWrapper<SupplierDelivery> queryWrapper = new QueryWrapper<SupplierDelivery>().checkSqlInjection();
        //数据归属
        queryWrapper.lambda().eq(SupplierDelivery::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierDeliveryPageParam.getSortField(), supplierDeliveryPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierDeliveryPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierDeliveryPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierDeliveryPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierDelivery::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierDeliveryAddParam supplierDeliveryAddParam) {
        SupplierDelivery supplierDelivery = BeanUtil.toBean(supplierDeliveryAddParam, SupplierDelivery.class);
        //数据归属
        supplierDelivery.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierDelivery);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierDeliveryEditParam supplierDeliveryEditParam) {
        SupplierDelivery supplierDelivery = this.queryEntity(supplierDeliveryEditParam.getId());
        BeanUtil.copyProperties(supplierDeliveryEditParam, supplierDelivery);
        this.updateById(supplierDelivery);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierDeliveryIdParam> supplierDeliveryIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierDeliveryIdParamList, SupplierDeliveryIdParam::getId));
    }

    @Override
    public SupplierDelivery detail(SupplierDeliveryIdParam supplierDeliveryIdParam) {
        return this.queryEntity(supplierDeliveryIdParam.getId());
    }

    @Override
    public SupplierDelivery queryEntity(String id) {
        SupplierDelivery supplierDelivery = this.getById(id);
        if(ObjectUtil.isEmpty(supplierDelivery)) {
            throw new CommonException("发货方式不存在，id值为：{}", id);
        }
        return supplierDelivery;
    }
}
